import { create, update, remove, query, deleteData } from '../../../../../framework/service/index';
import { ToastTips } from '../../../../../common/toastTips/tosat';
import { routerRedux } from 'dva/router';

export default {

  namespace: 'setWorkExperience',

  state: {
    date: [],
    id:0,
    selfEvaluate: '',
    entryTime:'', //入职时间
    dimissionTime: '',  //离职时间
    workCompany:'', //工作公司
    workPost:'', //工作岗位
    description:'', //工作描述
    pageStatus:'add',
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if(location.pathname === '/set-work-experience') {
          // dispatch({
          //   type: 'query',
          // })
        }
      })
    },
  },

  effects: {
    *query({ payload }, { call, put }) {

    },

    *addWorkExperienceInfo({ payload }, { call, put, select }) {

      const {entryTime, dimissionTime,
        workCompany, workPost, description} = yield select (({setWorkExperience}) => setWorkExperience);

      const dataInfo = {
        entryTime,
        dimissionTime,
        workCompany,
        workPost,
        description
      }
      // console.log('dataInfo',dataInfo);
      // return;
      const { code, data, message } = yield call(create, '/api/applicant/workExperience', {...dataInfo});

      if(code == 200){
        ToastTips('保存成功', 2000, null);
        yield put(routerRedux.goBack());
      }else{
        ToastTips('保存失败', 2000, null);
      }

    },

    *editWorkExperienceInfo({ payload }, { call, put, select }) {

      const {id, entryTime, dimissionTime,
        workCompany, workPost, description} = yield select (({setWorkExperience}) => setWorkExperience);

      const dataInfo = {
        id,
        entryTime,
        dimissionTime,
        workCompany,
        workPost,
        description
      }

      // console.log('dataInfo',dataInfo);
      // return;
      const { code, data, message } = yield call(update, `/api/applicant/workExperience/${id}`, {...dataInfo});
      if(code == 200){
        ToastTips('修改成功', 2000, null);
        yield put(routerRedux.goBack());
      }else{
        ToastTips('修改失败', 2000, null);
      }
    },

    *deleteWorkExperience({ payload }, { call, put, select }) {
      const { id } = payload;
      const { code, data, message } = yield call(deleteData, `/api/applicant/workExperience/${id}`, {});
      if(code == 200){
        ToastTips('删除成功', 2000, null);
        yield put(routerRedux.goBack());
      }else{
        ToastTips('删除失败', 2000, null);
      }
    },
  },

  reducers: {
    querySuccess(state, action) {
      return { ...state, ...action.payload };
    },
  }

};
